/*
 * Copyright (c) 2005, 2022, EVECOM Technology Co.,Ltd. All rights reserved.
 * EVECOM PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.ruoyi.web.controller.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.community.domain.CityArea;
import com.ruoyi.community.domain.HighMid;
import com.ruoyi.community.domain.ProvinceCity;
import com.ruoyi.community.domain.Region;
import com.ruoyi.community.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * <P><B>Description: </B>TODO 添加描述</P>
 * Revision Trail: (Date/Author/Description)
 * 2022/3/11 LuoYi CREATE
 *
 * @author LuoYi
 * @version 1.0
 */
@Component("highmid")
public class QuartzTask {
    @Autowired
    private IRegionService regionService;
    @Autowired
    private RedisCache redisCache;

    public void wxs(){
        String url="http://api.tianapi.com/ncov/index";
        String key="132c82ea9c421c7706b5feae9433060e";
        String date = DateUtils.getDate();
        /*name1=value1&name2=value2*/
        String param="key="+key+"&date="+date;
        String body = HttpUtils.sendGet(url, param);

        JSONObject li= JSON.parseObject(body);

        List<String> high= li.getJSONArray("newslist").
                getJSONObject(0).getJSONObject("riskarea").
                getJSONArray("high").toJavaList(String.class);
        List<String> mid= li.getJSONArray("newslist").
                getJSONObject(0).getJSONObject("riskarea").
                getJSONArray("mid").toJavaList(String.class);

        HighMid highMid = new HighMid();
        List<ProvinceCity> pc = getlist(high);
        int count=0;
        for (ProvinceCity p : pc) {
            count=count+p.getCount();
//            if(p.getProvince().equals("山东省")){
//                for (CityArea c: p.getCity()) {
//                    if(c.getCity().equals("青岛市")){
//                        c.getArea().add("山东省青岛莱西市龙口东路18号安居小区");
//                    }
//                }
//            }

        }
        highMid.setHigh(pc);
        highMid.setMid(getlist(mid));
        highMid.setMidCount(mid.size());
        highMid.setHighCount(count);
        redisCache.setCacheObject("hm",highMid);
    }

    public List<ProvinceCity> getlist(List<String> high){

        Region region=new Region();
        region.setPid(0);
        List<Region> province = regionService.selectRegionList(region);
        List<ProvinceCity> pc = new ArrayList<>();
        for (Region p : province) {
            Region regionCity=new Region();
            regionCity.setPid(p.getId().intValue());
            List<Region> city = regionService.selectRegionList(regionCity);
            List<CityArea> ca=new ArrayList<>();
            for (Region c : city) {
                // System.err.println(c.toString());
                List<String> area=new ArrayList<>();
                String qindao="";
                for (String h : high) {
                    if(h.indexOf(p.getName()+c.getName())==0){
                        area.add(h);
                    }
                    else if(p.getName().equals("北京市")&&h.indexOf(p.getName())==0){
                        area.add(h);
                    }
                    else if(p.getName().equals("天津市")&&h.indexOf(p.getName())==0){
                        area.add(h);
                    }
                    else if(p.getName().equals("上海市")&&h.indexOf(p.getName())==0){
                        area.add(h);
                    }
                    else if(p.getName().equals("重庆市")&&h.indexOf(p.getName())==0){
                        area.add(h);
                    }
//                    if(h.indexOf("山东省青岛莱西市龙口东路18号")==0){
//                        qindao=h;
//                    }
                }
                if(area.size()>0){
                    CityArea ci=new CityArea();
                    ci.setCity(c.getName());
                    ci.setArea(area);
                    ci.setCount(area.size());
                    ca.add(ci);
                }

            }
            if(ca.size()>0){
                ProvinceCity provinceCity = new ProvinceCity();
                provinceCity.setProvince(p.getName());
                provinceCity.setCity(ca);

                int count=0;
                for (CityArea cityArea : ca) {
                    count =count + cityArea.getArea().size() ;
                }
                provinceCity.setCount(count);
                pc.add(provinceCity);
            }

        }

        return pc;


    }




}
